﻿using Newtonsoft.Json;
using StrategyBox.Factors;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace StrategyBox
{
    [Serializable]
    public class FactorModel
    {
        [JsonProperty(PropertyName = "_id")]
        public string ID { get; set; }

        [JsonProperty(PropertyName = "base_factors")]
        public List<string> BaseFactors { get; set; }

        [JsonProperty(PropertyName = "begin_date")]
        public string BeginDate { get; set; }

        [JsonProperty(PropertyName = "bound_values")]
        public Dictionary<string, List<double>> BoundValues { get; set; }

        [JsonProperty(PropertyName = "coeff")]
        public Dictionary<string, double> Coeff { get; set; }

        [JsonProperty(PropertyName = "end_date")]
        public string EndDate { get; set; }

        [JsonProperty(PropertyName = "forward_time")]
        public double ForwardTime { get; set; }

        [JsonProperty(PropertyName = "instrument_id")]
        public string InstrumentId { get; set; }

        [JsonProperty(PropertyName = "intercept")]
        public double Intercept { get; set; }

        [JsonProperty(PropertyName = "module")]
        public Dictionary<string, string> Module { get; set; }

        [JsonProperty(PropertyName = "path")]
        public Dictionary<string, string> Path { get; set; }

        [JsonProperty(PropertyName = "scaler_id")]
        public string ScalerId { get; set; }

        [JsonProperty(PropertyName = "update_time")]
        public string UpdateTime { get; set; }

        public List<LinearFactor> Factors { get; set; } = new List<LinearFactor>();

        public void DeserializeFactor()
        {
            foreach (var data in BaseFactors)
            {
                var factor = new LinearFactor();
                factor.FactorId = int.Parse(Regex.Match(data, "MId(.*?)_").Groups[1].Value);
                factor.InstrumentId = Regex.Match(data, "IId(.*?)_").Groups[1].Value;
                factor.Interval = int.Parse(Regex.Match(data, "MInt(.*?)_").Groups[1].Value);
                factor.BackN = int.Parse(Regex.Match(data, "MBk(.*?)\\Z").Groups[1].Value);

                factor.Up = BoundValues[data][0];
                factor.Down = BoundValues[data][1];

                factor.Coeff = Coeff[data];
                factor.Module = Module[data];
                factor.FactorPath = Path[data];
                Factors.Add(factor);
            }

        }
    }
}
